﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SaveSation.Utils
{
    internal class CommonUtil
    {
        /// <summary>
        /// 确保文件路径已存在
        /// </summary>
        /// <param name="dirpath"></param>
        /// <returns></returns>
        [DllImport("dbghelp.dll", EntryPoint = "MakeSureDirectoryPathExists", CallingConvention = CallingConvention.StdCall)]
        public static extern long MakeSureDirectoryPathExists(string dirpath);

        private const int gbsize = 1024 * 1024 * 1024;
        private const int mbsize = 1024 * 1024;
        private const int kbsize = 1024;
        public static string GetSizeDesc(long size)
        {
            if (size > gbsize)
                return $"{(((double)size) / gbsize).ToString("f2")} GB";
            else if (size > mbsize)
                return $"{(((double)size) / mbsize).ToString("f2")} MB";
            else if (size > kbsize)
                return $"{(((double)size) / kbsize).ToString("f2")} KB";
            else
                return size + " B";
        }
    }
}
